import {Component, OnInit, Input, ViewChild, ElementRef} from '@angular/core';
import {CONSTANT} from "../../service/constant.service";
import {ROUTE} from "../../service/route.service";
import {LocalStorage} from "../../app.local-storage";
import SignaturePad from 'signature_pad';
import {CONTRACT} from "../../service/contract.service";
import {ActivatedRoute} from "@angular/router";
import {WfToast} from "../../utils/toast.utils";
import {IMAGEAPI} from "../../service/Image.api.service";
import {CheckStandModel} from "../../model/checkstand.model";

@Component({
  selector: 'app-contract-view',
  templateUrl: './contract-view.component.html',
  styleUrls: ['./contract-view.component.scss']
})
export class ContractViewComponent implements OnInit{
  @ViewChild('signPad') signPadCanvas: ElementRef;
  reqData:any={}
  signPad: any;
  progressBarList:any=this.constant.progressBarList;
  wordInfo:any={};
  contract:any={};
  isConfiguresEmpty:boolean=true;
  contractId:any;
  uuid:any;
  ngOnInit() {

    this.getdata();
    // 签名组件
    this.signPad = new SignaturePad(this.signPadCanvas.nativeElement);
  }

  constructor(private constant:CONSTANT,
              private route:ROUTE,
              private localStorage: LocalStorage,
              private contractApi:CONTRACT,
              private routeInfo:ActivatedRoute,
              private toast:WfToast,
              private ImageApi:IMAGEAPI) {
     this.reqData = this.localStorage.getObject('wordFormData');

  }

  getdata(){
    this.routeInfo.params.subscribe((param:any)=>{
      this.contractId = param.contractId;
      this.contractApi.getContractInfo(this.reqData,(res:any)=>{
        this.wordInfo = res;
        this.wordInfo.leaseStart = this.constant.transformDate(this.wordInfo.leaseStart);
        this.wordInfo.leaseEnd = this.constant.transformDate(this.wordInfo.leaseEnd);
        this.wordInfo.cotenantWords=this.localStorage.getObject('cotenantList');
        if(this.wordInfo.configures.length!=0){
          this.isConfiguresEmpty=false;
        }else {
          this.isConfiguresEmpty=true;
        }
      })
    });
  }

  goToConfirmPaymentView(){
    if(this.signPad.isEmpty()){
      this.toast.msg('您还未签名哟！请签名！');
      return false;
    }else{
      let param:any = {
        data: this.signPad.toDataURL('image/png')
      }
      this.contractApi.loadContract(this.reqData,v=>{
        this.ImageApi.base64Upload(param,(res:any)=>{
          let resData:any={
            signPicture:res.saveUrl,
            id:v.contractId
          }
          this.contractApi.getConfirm(resData,(res:any)=>{
            this.toast.msg('签约成功，请确认支付！');
            console.log(resData.id);
            let data:any={
              id:resData.id,
              uuid:this.uuid
            }

            let checkstand = new CheckStandModel(v.billNo,v.billContent,v.money,v.contractId,'personal/myrenting')
            checkstand.setAbnormalProgress();
            this.localStorage.setObject('checkstand',checkstand);
            this.route.goConfirmPaymentView(v.billNo);
          })
        })
      })

    }
  }
}
